-- Winter Anathema (c) v.v.balashoff
local Color = require('libs/colorlibrary/colorlibrary')
local Domain = require('leviathan/ECS/Domain')
local Entity = require('leviathan/ECS/Entity')
local i18n = require('libs/i18n')
local MusicComponent = require('leviathan/ECS/Audio/Components/MusicComponent')
local MusicSystem = require('leviathan/ECS/Audio/Systems/MusicSystem')
local RectangleComponent = require('leviathan/ECS/UI/Components/RectangleComponent')
local SnowComponent = require('leviathan/ECS/UI/Components/SnowComponent')
local TextComponent = require('leviathan/ECS/UI/Components/TextComponent')
local TextureComponent = require('leviathan/ECS/UI/Components/TextureComponent')
local UIRenderSystem = require('leviathan/ECS/UI/Systems/RenderSystem')
local UISnowSystem = require('leviathan/ECS/UI/Systems/SnowSystem')

local DomainBackground = Domain:derive('DomainBackground')

function DomainBackground:new(world)
    -- call parent constructor
    DomainBackground.super.new(self, world, 'MainMenuBackground')
    -- screen dimensions
    local screenW, screenH = love.window.getMode()
    -- get managers
    local ImagesManager = world:getManager('ImagesManager')
    local FontsManager = world:getManager('FontsManager')
    -- background
    local bg = Entity(self, 'MainMenuBackground', 0, 0, screenW, screenH)
    local bgRectangleComp = RectangleComponent('fill', 0, 0, screenW, screenH, 0, 0, Color.white)
    bg:addComponent(bgRectangleComp)
    -- background image
    local bgTexture = ImagesManager:getImageTexture('MainMenuBackground')
    local bgimg = Entity(self, 'MainMenuBackground', 0, 0, screenW, screenH)
    bgimg:setPriority(510)
    local bgTextureComp = TextureComponent(bgTexture.texture, 0, 0, screenW, screenH, 'center', 'bottom', true)
    bgimg:addComponent(bgTextureComp)
    -- project name
    local prjSizeX = math.floor(screenW * 0.62)
    local prjSizeY = screenH * 0.2 -- 20% of screen height
    local fontTitle = FontsManager:getFont('Rusted', prjSizeY)
    local prj = Entity(self, 'ProjectName', screenW * 0.03, screenH * 0.05, prjSizeX, prjSizeY)
    prj:setPriority(530)
    local name = i18n.translate('ProjectName')
    local prjNameComp = TextComponent('ProjectName', 0, 0, nil, nil, name, fontTitle, Color.black, 'left')
    prj:addComponent(prjNameComp)
    -- snow
    -- calculate snow density depending on screen size
    local sn = Entity(self, 'MainMenuBackground', 0, 0, screenW, screenH)
    sn:setPriority(520)
    local snowflakes = screenH * (screenW / screenH) * 1.7
    local upTime = 0.018 -- ~snowflakes/130000
    local snow = SnowComponent(snowflakes, 0, 0, screenW, screenH, upTime)
    sn:addComponent(snow)
    -- entity: music
    local musicPlayer = Entity(self, 'MainMenuMusic')
    local musicComp = MusicComponent('MainMenuMusic')
    musicPlayer:addComponent(musicComp)
    -- music
    local audio = world:getDomain('Audio')
    audio:playMusic('MainMenuMusic')
    -- systems
    local render = UIRenderSystem(self)
    local snowS = UISnowSystem(self)
    -- adding systems
    DomainBackground:addSystem(self, render)
    DomainBackground:addSystem(self, snowS)
end

return DomainBackground
